package net.qhzw.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.admin.entity.Personnel;
import net.qhzw.admin.entity.PersonnelLog;
import net.qhzw.admin.mapper.PersonnelLogMapper;
import net.qhzw.admin.pojo.PersonnelLogVo;
import net.qhzw.admin.pojo.PersonnelVo;
import net.qhzw.admin.query.PersonnelLogQuery;
import net.qhzw.admin.service.PersonnelLogService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PersonnelLogServiceImpl implements PersonnelLogService {


    @Autowired
    private PersonnelLogMapper personnelLogMapper;

    @Value("${config.imgage.view}")
    private String imageView;

    /***
     * 获取资料变更列表
     * @return
     */
    @Override
    public JsonResult personnelLogList(PersonnelLogQuery query) {
        log.info("query{}, ", JSON.toJSONString(query));
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        if (query.getRecordTime() != null && query.getRecordTime().length > 0) {
            query.setStartTime(query.getRecordTime()[0]);
            query.setEndTime(query.getRecordTime()[1]);
        }
        List<PersonnelLogVo> personnelLogList = personnelLogMapper.selectList(query);
        for (PersonnelLogVo personnelLogVo : personnelLogList) {
            if (personnelLogVo.getRecordAfter() != null && !StringUtils.isEmpty(personnelLogVo.getRecordAfter())) {
                PersonnelVo personnelAfter = JSONObject.parseObject(personnelLogVo.getRecordAfter(), PersonnelVo.class);
                if (personnelAfter.getPhoto() != null) {
                    personnelAfter.setPhoto(imageView + personnelAfter.getPhoto());
                }
                personnelLogVo.setAfterObj(personnelAfter);

            }
            if (personnelLogVo.getRecordBefore() != null && !StringUtils.isEmpty(personnelLogVo.getRecordBefore())) {
                PersonnelVo personnelBefore = JSONObject.parseObject(personnelLogVo.getRecordBefore(), PersonnelVo.class);
                if (personnelBefore.getPhoto() != null) {
                    personnelBefore.setPhoto(imageView + personnelBefore.getPhoto());
                }
                personnelLogVo.setBeforeObj(personnelBefore);
            }
        }
        PageInfo<PersonnelLogVo> pageInfo = new PageInfo<>(personnelLogList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return JsonResult.success(resultMap);
    }
}
